ALTER PROCEDURE [dbo].[spe_sms_T_IVROBRecord_A00011]
AS
BEGIN
	declare @tel1 varchar(50)
	declare @tel2 varchar(50)
	declare @id int
	declare @count int
	declare @r_guid varchar(36)
	declare @cj_guid varchar(36)
	declare @ysdz varchar(200)
	declare @t_count int
	declare @sms_guid varchar(36)
	declare @ob_count int 
	declare @tel_1_guid varchar(36)
	declare @tel_2_guid varchar(36),@t_obendtime datetime

	select @t_count=COUNT(*) from ucis_autoob where status=1 and type_id='A00011'
	if(@t_count>0)
	begin
		select top 1 @sms_guid=sms_guid from ucis_autoob where type_id='A00011'
		if @sms_guid!=''
		begin
			update ucis_sms_monitoring set IVROB_starttime=GETDATE()  where SMS_GUID=@sms_guid
			begin
			declare cur_tel1 cursor for select tel_1,count(*)  from CJ_Comprehensive_UCIS With (Nolock) where sms_guid=@sms_guid and type_id='A00011' and tel_1 is not null and tel_1 != '' and smsstatus!='DELIVRD' group by tel_1 
			--处理电话1
			open cur_tel1
			fetch next from cur_tel1 into @tel1,@count
			while @@fetch_status=0
			begin
				if(@count>1)	
				begin
					set @r_guid=newid()
					insert into T_IVROBRecord(REC_GUID,Phone1,StringField2,CurrentPhoneDialCount,CurrentPhoneDialLimit,ExpectedStartTime,NextCallTime,status,sms_guid,StringField10) 
					values(@r_guid,@tel1,@count,0,10,GETDATE(),GETDATE(),0,@sms_guid,'UCIS')
					declare cur_tel_1_guid cursor for 
					select cj_guid from CJ_Comprehensive_UCIS With (Nolock) where sms_guid=@sms_guid and type_id='A00011' and tel_1 is not null and tel_1 != '' 
					and smsstatus!='DELIVRD' and tel_1=@tel1
					open cur_tel_1_guid
					fetch next from cur_tel_1_guid into @tel_1_guid
					while @@fetch_status=0
					begin
						update CJ_Comprehensive_UCIS set t_result=@r_guid where cj_guid = @tel_1_guid
						if @@error<>0
						begin
							goto nextpoint3
						end
						nextpoint3:
						fetch next from cur_tel_1_guid into @tel_1_guid
					end
					close cur_tel_1_guid
					deallocate cur_tel_1_guid
				end
				else
				begin
					select @cj_guid=cj_guid,@ysdz=ysdz from CJ_Comprehensive_UCIS With (Nolock) where sms_guid=@sms_guid and type_id='A00011' and tel_1=@tel1 and smsstatus!='DELIVRD'
					set @r_guid=newid()
					insert into T_IVROBRecord(REC_GUID,Phone1,StringField1,CurrentPhoneDialCount,CurrentPhoneDialLimit,ExpectedStartTime,NextCallTime,status,sms_guid,StringField10) 
					values(@r_guid,@tel1,@ysdz,0,10,GETDATE(),GETDATE(),0,@sms_guid,'UCIS')
					update CJ_Comprehensive_UCIS set t_result=@r_guid where cj_guid=@cj_guid	
				end
			fetch next from cur_tel1 into @tel1,@count
			end;
			close cur_tel1
			deallocate cur_tel1
			end
			
			begin
			declare cur_tel2 cursor for select tel_2,count(*)  from CJ_Comprehensive_UCIS With (Nolock) where sms_guid=@sms_guid and type_id='A00011' and tel_2 is not null and tel_2 != '' and smsstatus!='DELIVRD' group by tel_2 
			--处理电话2
			open cur_tel2
			fetch next from cur_tel2 into @tel2,@count
			while @@fetch_status=0
			begin
				set @r_guid=''
				if(@count>1)	
				begin
					set @r_guid=newid()
					insert into T_IVROBRecord(REC_GUID,Phone1,StringField2,CurrentPhoneDialCount,CurrentPhoneDialLimit,ExpectedStartTime,NextCallTime,status,sms_guid,StringField10) 
					values(@r_guid,@tel2,@count,0,10,GETDATE(),GETDATE(),0,@sms_guid,'UCIS')
					declare cur_tel_2_guid cursor for 
					select cj_guid from CJ_Comprehensive_UCIS With (Nolock) where sms_guid=@sms_guid and type_id='A00011' and tel_2 is not null and tel_2 != '' 
					and smsstatus!='DELIVRD' and tel_2=@tel2
					open cur_tel_2_guid
					fetch next from cur_tel_2_guid into @tel_2_guid
					while @@fetch_status=0
					begin
						update CJ_Comprehensive_UCIS set t_result1=@r_guid where cj_guid = @tel_2_guid
						if @@error<>0
						begin
							goto nextpoint4
						end
						nextpoint4:
						fetch next from cur_tel_2_guid into @tel_2_guid
					end
					close cur_tel_2_guid
					deallocate cur_tel_2_guid
				end
				else
				begin
					select @cj_guid=cj_guid,@ysdz=ysdz from CJ_Comprehensive_UCIS With (Nolock) where sms_guid=@sms_guid and type_id='A00011' and tel_2=@tel2	and smsstatus!='DELIVRD'	
					set @r_guid=newid()
					insert into T_IVROBRecord(REC_GUID,Phone1,StringField1,CurrentPhoneDialCount,CurrentPhoneDialLimit,ExpectedStartTime,NextCallTime,status,sms_guid,StringField10) 
					values(@r_guid,@tel2,@ysdz,0,10,GETDATE(),GETDATE(),0,@sms_guid,'UCIS')
					update CJ_Comprehensive_UCIS set t_result1=@r_guid where cj_guid=@cj_guid
				end			
			fetch next from cur_tel2 into @tel2,@count
			end;
			close cur_tel2
			deallocate cur_tel2
			end
				
			begin
				delete from ucis_autoob where type_id='A00011' and sms_guid=@sms_guid
				select @ob_count=COUNT(*) from T_IVROBRecord where sms_guid=@sms_guid
				update ucis_sms_monitoring set IVROB_endtime=GETDATE(), ivr_ob_count=@ob_count where SMS_GUID=@sms_guid
				update UCIS_CJ_result set ob_count=@ob_count where cj_guid=@sms_guid
				update OB_CAMPAIGN set runningflag=1 where campaign_id='FE84D9E5-0CA1-4346-93F6-1592B745C88B'
				select @t_obendtime=ob_endtime from ucis_smssendplan where REC_GUID=@sms_guid
				insert into UCIS_OB_end values(@sms_guid,1,'A00011',@t_obendtime)--如果ucis_smssendplan里的ob_endtime为空，应取当前时间
			end
			--数据回写到44（部门代码为集团需进行电话催缴，电话数据生成完毕之后，同步数据到44）
			begin
				insert into ucis_data_to_44 values(@sms_guid,'A00011',GETDATE())
			end
		end
	END
end